home *** CD-ROM | disk | FTP | other *** search
- // tester for adsp
- // ©1989 bet
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <String.h>
- #include <Strings.h>
- #include <Devices.h>
- #include <Files.h>
- #include <AppleTalk.h>
- #include <Signal.h>
- #include <Errors.h>
-
- typedef struct xmitHeader {
- short numFiles;
- long dfSize;
- long rfSize;
- FInfo fInfo;
- char fName[32];
- } xmitHeader;
-
- static sig_atomic_t flag;
- short connRefNum,fRefNum;
- AddrBlock aBlock;
- AddrBlock aBlock;
- ATPParamBlock atpBlock;
- MPPParamBlock nbpPB;
- Boolean server = false;
- BDSType bds; // block data stuff for ATP responses
-
- #define qSize 600
- #define dataBufSiz 512
-
- void closeATP()
- {
- ATPParamBlock atpBlock;
- short error;
- // close up ATP
- atpBlock.ATPatpSocket = connRefNum;
- if (error = PCloseATPSkt(&atpBlock,false))
- printf("### error %d closing ATP\n",error);
- }
-
- long countBlocks(dataSize,blockSize)
- long dataSize,blockSize;
-
- {
- // we need one extra block if there is a partial block at the end.
- return(blockSize / dataSize + (blockSize % dataSize ?1:0));
- }
-
-
- void rmveName()
- {
- short error;
- char ourEName[99];
-
- // remove name
- NBPSetEntity(ourEName,"\pBlackHole","\pTalkServer","\p*");
- nbpPB.NBPentityPtr = ourEName;
- nbpPB.NBPinterval = 5;
- nbpPB.NBPcount = 2;
- if (error = PRemoveName(&nbpPB,false))
- printf("### Error %d removing server name.\n",error);
- }
-
- void exProc()
- {
- short error;
-
- if (server)
- {
- rmveName();
- closeATP();
- }
- if (fRefNum)
- {
- printf("frefnum is %d",fRefNum);
- if(error = FSClose(fRefNum))
- printf("### Error %d closing open file in exProc.\n",error);
- }
- }
-
- void getFileFork(numBlocks)
- long numBlocks;
-
- {
- char dataBuf[8][dataBufSiz];
- short error;
- long dataWritten;
-
- for(; numBlocks; numBlocks--)
- {
- // set up bds
- // we set these up but only use first
- // for (count=0; count<8; count++)
- {
- bds[0].buffSize = dataBufSiz;
- bds[0].dataSize = dataBufSiz;
- bds[0].buffPtr = dataBuf[0];
- }
-
- // set up rest
- atpBlock.ATPatpFlags = 0;
- atpBlock.ATPaddrBlock = aBlock;
- atpBlock.ATPreqLength = 6;
- atpBlock.ATPreqPointer = "Shit!";
- atpBlock.ATPbdsPointer = (Ptr)bds;
- atpBlock.ATPnumOfBuffs = 1;
- atpBlock.ATPtimeOutVal = 0;
- atpBlock.ATPretryCount = 5;
- if (error = PSendRequest(&atpBlock,false))
- {
- printf("### Error %d sending request for data.\n",error);
- exit(error);
- }
-
- // write out the data we just got
- dataWritten = bds[0].dataSize;
- if (error = FSWrite(fRefNum,&dataWritten,bds[0].buffPtr))
- {
- printf("### Error %d writing data for file.\n",error);
- exit(error);
- }
- }
-
- if(error = FSClose(fRefNum))
- {
- printf("### Error %d closing open file on exit.\n",error);
- exit(error);
- }
- fRefNum = 0;
- }
-
- void putFileFork(numBlocks)
- long numBlocks;
-
- {
- char dataBuf[8][dataBufSiz];
- short error;
- long dataRead;
-
- for(; numBlocks; numBlocks--)
- {
- // set up bds
- // we set these up but only use first
- // for (count=0; count<8; count++)
- {
- bds[0].buffSize = dataBufSiz;
- bds[0].dataSize = dataBufSiz;
- bds[0].buffPtr = dataBuf[0];
- }
-
- // anybody want anything? (somebody always wants something!)
- atpBlock.ATPatpSocket = connRefNum;
- bds[0].dataSize = atpBlock.ATPreqLength = dataBufSiz;
- atpBlock.ATPreqPointer = dataBuf[0];
- if (error = PGetRequest(&atpBlock,true))
- {
- printf("### Error %d during socket request.\n",error);
- // need to make sure other node doesn't hang here.
- exit(error);
- }
- while ((error = atpBlock.ATPioResult) > 0);
- if (error)
- {
- printf("### Error %d on return from getBlock GetRequest.\n",error);
- exit(error);
- }
-
-
- // read in some data, try for a full block, don't worry if not
- dataRead = dataBufSiz;
- if (error = FSRead(fRefNum,&dataRead,bds[0].buffPtr))
- if (error == eofErr)
- // if we didn't get a full block, indicate it in the bds structure,
- // and make sure to set the EOM flag if numBlocks != 1
- bds[0].buffSize = dataRead;
- else
- {
- printf("### Error %d writing data for file.\n",error);
- exit(error);
- }
-
- // set up rest
- atpBlock.ATPatpSocket = connRefNum;
- atpBlock.ATPbdsPointer = (Ptr)bds;
- atpBlock.ATPnumOfBuffs = 1;
- atpBlock.ATPbdsSize = 1;
- if (error = PSendResponse(&atpBlock,false))
- {
- printf("### Error %d sending request for data.\n",error);
- exit(error);
- }
- }
-
- if(error = FSClose(fRefNum))
- {
- printf("### Error %d closing open file on exit.\n",error);
- exit(error);
- }
- fRefNum = 0;
- }
-
-
-
- main(argc,argv)
- short argc;
- char *argv[];
-
- {
- short error,vRefNum;
- long numBlocks;
- char ourEName[108],*sPtr,*tempPtr;
- char dataBuf[dataBufSiz];
- xmitHeader ourHeader;
- char pfName[255]; // pascal file name buffer for FS calls
-
- // check if user asked for server, i.e. no filenames
- if ((ourHeader.numFiles = argc-1) > 0)
- server = true;
-
- // set exit proc
- atexit(exProc);
-
- // open drivers
- if (error = ATPLoad()) {printf("error %d opening ATP\n",error); exit(error);}
-
- // do files setup
- Debugger();
- GetVol(pfName,&vRefNum);
-
- // depending on if we are the sender of the file, either advertise that we
- // are available, or look for an advertisement and solicit data.
- if (server)
- {
- SysBeep(5);
- // set up ATP responder socket
- aBlock.aNet = 0;
- aBlock.aNode = 0;
- aBlock.aSocket = 0;
- atpBlock.ATPatpSocket = 0;
- atpBlock.ATPaddrBlock = aBlock;
- atpBlock.ATPioCompletion = nil;
- if(error = POpenATPSkt(&atpBlock,false))
- {
- printf("### Error %d opening responder socket.\n",error);
- exit(error);
- }
-
- connRefNum = atpBlock.ATPatpSocket;
-
- // register our name
- NBPSetNTE(ourEName,"\pBlackHole","\pTalkServer","\p*",connRefNum);
- nbpPB.NBPentityPtr = ourEName;
- nbpPB.NBPinterval = 2;
- nbpPB.NBPcount = 20;
- nbpPB.NBPverifyFlag = true;
- if(error = PRegisterName(&nbpPB,false))
- {
- printf("### Error %d registering name.\n",error);
- exit(error);
- }
-
- // we have a taker, issue a GetRequest to find out what he wants
- atpBlock.ATPatpSocket = connRefNum;
- atpBlock.ATPreqLength = dataBufSiz;
- atpBlock.ATPreqPointer = dataBuf;
- if (error = PGetRequest(&atpBlock,true))
- {
- printf("### Error %d during socket request.\n",error);
- // need to make sure other node doesn't hang here.
- exit(error);
- }
- while ((error = atpBlock.ATPioResult) > 0);
- if (error)
- {
- printf("### Error %d after socket request.\n",error);
- // need to make sure other node doesn't hang here.
- exit(error);
- }
-
- // of course he wants files, so start sending them.
- // general procedure is to prepare the header block with
- // the first file's info, send it, then decrement the numFiles
- // field of the xmitHeader record.
- for(;ourHeader.numFiles;ourHeader.numFiles--)
- {
- // set up file names for use
- strcpy(pfName,sPtr = argv[ourHeader.numFiles]);
- c2pstr(pfName);
- printf("Transfering file %s...",sPtr);
-
- if(error = FSOpen(pfName,vRefNum,&fRefNum))
- {
- printf("### Error %d opening file %s.\n",error,sPtr);
- // need to make sure other node doesn't hang here.
- exit(error);
- }
- if(error = GetEOF(fRefNum,&ourHeader.dfSize))
- {
- printf("### Error %d finding data EOF on file %s.\n",error,sPtr);
- // need to make sure other node doesn't hang here.
- exit(error);
- }
-
- if(error = FSClose(fRefNum))
- {
- printf("### Error %d closing open file on exit.\n",error);
- exit(error);
- }
- fRefNum = 0;
-
- if(error = OpenRF(pfName,vRefNum,&fRefNum))
- {
- printf("### Error %d opening file %s.\n",error,sPtr);
- // need to make sure other node doesn't hang here.
- exit(error);
- }
- if(error = GetEOF(fRefNum,&ourHeader.rfSize))
- {
- printf("### Error %d finding resource EOF on file %s.\n",error,sPtr);
- // need to make sure other node doesn't hang here.
- exit(error);
- }
-
- if(error = FSClose(fRefNum))
- {
- printf("### Error %d closing open file on exit.\n",error);
- exit(error);
- }
- fRefNum = 0;
-
- if(error = GetFInfo(pfName,vRefNum,&ourHeader.fInfo))
- {
- printf("### Error %d getting finder info on file %s.\n",error,sPtr);
- // need to make sure other node doesn't hang here.
- exit(error);
- }
-
- if (tempPtr = strrchr(sPtr,':'))
- strcpy(ourHeader.fName,tempPtr);
- else
- strcpy(ourHeader.fName,sPtr);
-
- // set up bds
- bds[0].buffSize = sizeof(xmitHeader);
- bds[0].dataSize = sizeof(xmitHeader);
- bds[0].buffPtr = (Ptr)&ourHeader;
-
- // send this response to make sure that the destination drive has space
- // for this file. If there is no space, try the next one.
- atpBlock.ATPatpSocket = connRefNum;
- atpBlock.ATPbdsPointer = (Ptr)bds;
- atpBlock.ATPnumOfBuffs = 1;
- atpBlock.ATPbdsSize = 1;
- if (error = PSendResponse(&atpBlock,false))
- {
- printf("### Error %d extracting name.\n",error);
- exit(error);
- }
-
- // start looping requests for data fork, next for res fork in actual tool
- // to send the file we will open the fork we want, and pass the file
- // refNum to a proc to do the actual reading and writing. This proc will
- // work for both data and resource forks, since the only difference is the
- // refNum.
-
- // first do data fork
- // we need one extra block if there is a partial block at the end.
- numBlocks = countBlocks(dataBufSiz,ourHeader.dfSize);
- if(error = FSOpen(pfName,vRefNum,&fRefNum))
- {
- printf("### Error %d opening source file data fork.\n",error);
- exit(error);
- }
-
- putFileFork(numBlocks);
-
- // now do resource fork.
- // again, we need one extra block if there is a partial block at the end.
- numBlocks = countBlocks(dataBufSiz,ourHeader.rfSize);
- if(error = OpenRF(pfName,vRefNum,&fRefNum))
- {
- printf("### Error %d opening source file data fork.\n",error);
- exit(error);
- }
-
- putFileFork(numBlocks);
-
- // tell user were done with that file!
- printf(" Done!\n");
- }
- }
- else
- {
- NBPSetEntity(ourEName,"\p=","\pTalkServer","\p*");
- nbpPB.NBPentityPtr = ourEName;
- nbpPB.NBPinterval = 2;
- nbpPB.NBPcount = 20;
- nbpPB.NBPretBuffPtr = dataBuf;
- nbpPB.NBPretBuffSize = dataBufSiz;
- nbpPB.NBPmaxToGet = 1;
- if (error = PLookupName(&nbpPB,false))
- {
- printf("### Error %d Looking up server.\n",error);
- exit(error);
- }
- if (error = NBPExtract(dataBuf,nbpPB.NBPnumGotten,1,(EntityName *)ourEName,&aBlock))
- {
- printf("### Error %d extracting name.\n",error);
- exit(error);
- }
-
- // found a source for our data, let's request some and see what we get.
- // the contents of our request doesn't matter as much as making the connection.
-
- // set up bds
- bds[0].buffSize = sizeof(xmitHeader);
- bds[0].dataSize = sizeof(xmitHeader);
- bds[0].buffPtr = (Ptr)&ourHeader;
-
- // set up rest
- atpBlock.ATPatpFlags = 0;
- atpBlock.ATPaddrBlock = aBlock;
- atpBlock.ATPreqLength = 6;
- atpBlock.ATPreqPointer = "Shit!";
- atpBlock.ATPbdsPointer = (Ptr)bds;
- atpBlock.ATPnumOfBuffs = 1;
- atpBlock.ATPtimeOutVal = 0;
- atpBlock.ATPretryCount = 5;
- if (error = PSendRequest(&atpBlock,false))
- {
- printf("### Error %d sending request for header.\n",error);
- exit(error);
- }
-
- // now we should issue requests for data fork, and then for resource fork
- // tell user which file we are transferring and create file.
- printf("Transfering file %s...",ourHeader.fName);
- strcpy(pfName,ourHeader.fName);
- c2pstr(pfName);
- if (error = Create(pfName,vRefNum,ourHeader.fInfo.fdCreator,ourHeader.fInfo.fdType))
- {
- printf("### Error %d creating new file.\n",error);
- exit(error);
- }
-
- // to create the file we will open the fork we want, and pass the file
- // refNum to a proc to do the actual reading and writing. This proc will
- // work for both data and resource forks, since the only difference is the
- // refNum.
-
- // first do data fork
- // we need one extra block if there is a partial block at the end.
- numBlocks = countBlocks(dataBufSiz,ourHeader.dfSize);
- if(error = FSOpen(pfName,vRefNum,&fRefNum))
- {
- printf("### Error %d opening new file data fork.\n",error);
- exit(error);
- }
-
- getFileFork(numBlocks);
-
- // now do resource fork.
- // again, we need one extra block if there is a partial block at the end.
- numBlocks = countBlocks(dataBufSiz,ourHeader.rfSize);
- if(error = OpenRF(pfName,vRefNum,&fRefNum))
- {
- printf("### Error %d opening new file data fork.\n",error);
- exit(error);
- }
-
- getFileFork(numBlocks);
-
- // finally set file attributes to the same that were in the source file.
- if (error = SetFInfo(pfName,vRefNum,&(ourHeader.fInfo)))
- {
- printf("### Error %d setting new file's file info.\n",error);
- exit(error);
- }
-
- // tell user were done with that file!
- printf(" Done!\n");
- }
- }
-